//
//  AlertController.swift
//  DSP
//
//  Created by tucici on 2024/5/23.
//

import UIKit
import TCCKit
enum AlertControllerStyle{
    case normal
    case input
}
class AlertController: DSAlertController {
    private var bgView : UIView!
    var msg : UILabel!
    var tips : UILabel!
    private var line : UIView!
    private var leftBtn : UIButton!
    private var rightBtn : UIButton!
    private var input : UITextField!
    var leftBlock : (()->Void)?
    var rightBlock : ((_ content:String)->Void)?
    var tipsBlock : (()->Void)?
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
    }
    override func initUI() {
        bgView = UIView()
        bgView.bgHexColor = "#FFFFFF"
        bgView.cornerRadius = 10.k_h
        
        getLabel(f: 16, lbl: &msg)
        getLabel(f: 12, lbl: &tips)
        getBtn(title: "Cancel", sender: &leftBtn)
        getBtn(title: "OK", sender: &rightBtn)
        
        func getLabel(f:CGFloat ,lbl:inout UILabel?){
            let label = lbl ?? UILabel()
            label.textHexColor = "#000000"
            label.font = font(f)
            label.numberOfLines = 0
            label.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
            label.setContentHuggingPriority(.defaultLow, for: .horizontal)
            lbl = label
        }
        
        func getBtn(title:String,  sender:inout UIButton?){
            let btn = UIButton(type: .custom)
            btn.setTitle(title, for: .normal)
            btn.textHexColor = "#000000"
            btn.textHightLightColor = "#999999"
            btn.titleLabel?.font = font(16)
            btn.addTarget(self, action: #selector(actionForBtn), for: .touchUpInside)
            btn.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
            btn.setContentHuggingPriority(.defaultHigh, for: .horizontal)
            sender = btn
        }
        
        line = UIView()
        line.bgHexColor = "#000000"
        
        input = UITextField()
        input.delegate = self
        input.font = font(14)
        input.textHexColor = "#000000"
        
        view.addSubview(bgView)
        bgView.addSubview(msg)
        bgView.addSubview(tips)
        bgView.addSubview(input)
        bgView.addSubview(line)
        bgView.addSubview(leftBtn)
        bgView.addSubview(rightBtn)
        
        bgView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(k_w(30))
            make.centerY.equalToSuperview()
        }
        
        msg.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(16)
            make.top.equalToSuperview().offset(30)
        }
        
        input.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(16)
            make.top.equalTo(msg.snp.bottom).offset(0)
            make.height.equalTo(30)
        }
        
        line.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(10)
            make.top.equalTo(input.snp.bottom).offset(0)
            make.height.equalTo(1)
        }
        
        tips.snp.makeConstraints { make in
            make.left.equalToSuperview().inset(16)
            make.top.equalTo(line.snp.bottom).offset(0)
            make.bottom.equalToSuperview().inset(0)
            make.height.equalTo(40)
        }
        
        rightBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().inset(16)
            make.bottom.equalToSuperview()
            make.top.equalTo(line.snp.bottom)
        }
        
        leftBtn.snp.makeConstraints { make in
            make.right.equalTo(rightBtn.snp.left).offset(-10)
            make.bottom.equalToSuperview()
            make.top.equalTo(line.snp.bottom)
            make.left.equalTo(tips.snp.right).offset(10)
        }
    }
    
    var style:AlertControllerStyle = .normal{
        didSet{
            input.isHidden = style == .normal
        }
    }
    var leftBtnTitle = "" {
        didSet{
            leftBtn.isUserInteractionEnabled = leftBtnTitle != ""
            leftBtn.setTitle(leftBtnTitle, for: .normal)
        }
    }
    
    var rightBtnTitle = "" {
        didSet{
            rightBtn.setTitle(rightBtnTitle, for: .normal)
        }
    }
    
    @objc func actionForBtn(sender:UIButton){
        dismiss(animated: true, completion: {[weak self]in
            sender == self?.leftBtn ? self?.leftBlock?() : self?.rightBlock?(self?.input.text ?? "")
            
        })
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)
        guard let touch = touches.first else { return }
        let touchLocation = touch.location(in: self.view)
        let targetViewLocation = self.view.convert(touchLocation, to: tips)
        if tips.bounds.contains(targetViewLocation) {
            dismiss(animated: false, completion: {[weak self] in
                self?.tipsBlock?()
            })
        }
    }
    
}

//MARK: - UITextFieldDelegate
extension AlertController : UITextFieldDelegate{
    
}
